<?php
include(SERVER_ROOT_PATH.'/include/config/ml_mail_config.php');

class ml_tool_sendmail
{
    /**
     * 发邮件
     *
     * @param string $from                //发件人邮箱
     * @param string $namefrom                //发件人名称
     * @param string $to                    //收件人邮箱
     * @param string $nameto                //收件人名称
     * @param string $subject                //标题
     * @param string $message                //邮件内容
     * @return array
     */
    function sendMessage($from, $namefrom, $to, $nameto, $subject, $message){
            
        
            /*your configuration here*/
            //$smtpServer = 'smtp.sina.cn'; //does not accept STARTTLS
            //$smtpServer = 'staff.sina.com.cn'; //'10.69.9.253';
            $smtpServer = ML_SMTP_HOST;//'10.69.9.253'; //'10.69.9.253';
            $port = ML_SMTP_PORT; // try 587 if this fails
            $timeout = ML_SMTP_TIMEOUT; //typical timeout. try 45 for slow servers

            $localhost = ''; //requires a real ip
            $username = ML_SMTP_SENDER;//'blogstaff@sina.com'; //your gmail account
            $password = ML_SMTP_PASSWORD;//'01223344'; //the pass for your gmail
            $newLine = "\r\n"; //var just for newlines
    
            /* 获取Ip*/
            $localhost = Tool_ip::getLocalLastIp(true ,false);
            
            /*  you shouldn't need to mod anything else */
    
            //connect to the host and port
            $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
            //echo $errstr." - ".$errno;
            $smtpResponse = fgets($smtpConnect, 4096);
            
            if(empty($smtpResponse))
            {
                //$output = "Failed to connect: $smtpResponse";
                //echo $output;
                return false;
            }
            else
            {
                $logArray['connection'] = "Connected to: $smtpResponse";
                //echo "connection accepted<br>".$smtpResponse."<p />Continuing<p />";
            }
            //you have to say HELO again after TLS is started
            fputs($smtpConnect, "HELO $localhost". $newLine);
            
            $smtpResponse = fgets($smtpConnect, 4096);
            
            $logArray['heloresponse2'] = "$smtpResponse";
    
            //request for auth login
            /*
            fputs($smtpConnect,"AUTH LOGIN" . $newLine);
            
            $smtpResponse = fgets($smtpConnect, 4096);
            
            $logArray['authrequest'] = "$smtpResponse";
    
            //send the username
            fputs($smtpConnect, base64_encode($username) . $newLine);
            
            $smtpResponse = fgets($smtpConnect, 4096);
            
            $logArray['authusername'] = "$smtpResponse";
    
            //send the password
            fputs($smtpConnect, base64_encode($password) . $newLine);
            
            $smtpResponse = fgets($smtpConnect, 4096);
            
            $logArray['authpassword'] = "$smtpResponse";
    */
            //email from
            fputs($smtpConnect, "MAIL FROM: <$from>" . $newLine);
            
            $smtpResponse = fgets($smtpConnect, 4096);
            
            $logArray['mailfromresponse'] = "$smtpResponse";
    
            //email to
            fputs($smtpConnect, "RCPT TO: <$to>" . $newLine);
            
            $smtpResponse = fgets($smtpConnect, 4096);
            
            $logArray['mailtoresponse'] = "$smtpResponse";
    
            //the email
            fputs($smtpConnect, "DATA" . $newLine);
            
            $smtpResponse = fgets($smtpConnect, 4096);
            
            $logArray['data1response'] = "$smtpResponse";
    
            //construct headers
            $headers = "MIME-Version: 1.0" . $newLine;
            $headers .= "Content-type: text/html; charset=utf-8" . $newLine;
            //$headers .= "To: $nameto <$to>" . $newLine;
            //$headers .= "From: $namefrom <$from>" . $newLine;
    
            //observe the . after the newline, it signals the end of message
            //fputs($smtpConnect, "To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
            
            $namefrom = '=?UTF-8?B?'.base64_encode($namefrom).'==?=';
            $subject = '=?UTF-8?B?'.base64_encode($subject).'==?=';
            fputs($smtpConnect, "To: $nameto<$to>\r\nFrom: $namefrom<$from>\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
            $smtpResponse = fgets($smtpConnect, 4096);
            
            $logArray['data2response'] = "$smtpResponse";
    
            // say goodbye
            fputs($smtpConnect,"QUIT" . $newLine);
            
            $smtpResponse = fgets($smtpConnect, 4096);
            
            $logArray['quitresponse'] = "$smtpResponse";
            $logArray['quitcode'] = substr($smtpResponse,0,3);
            fclose($smtpConnect);
            //a return value of 221 in $retVal["quitcode"] is a success            
   
            return($logArray);
    } // end function sendMessage
    
}
?>